$.validator.setDefaults({
	highlight: function (element) {
		$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    },success: function (element) {
    	element.closest('.form-group').removeClass('has-error').addClass('has-success');
    },errorElement: "span",
    errorPlacement: function (error, element) {
        if (element.is(":radio") || element.is(":checkbox")) {
            error.appendTo(element.parent().parent().parent());
        } else {
            error.appendTo(element.parent());
        }
    },errorClass: "help-block m-b-none",
    validClass: "help-block m-b-none"
});
var branch = function(){
	/**
	 * 初始化栏目表格
	 */
	var handleTable = function(){
		var ctable = $('.branchTable');
		var ccTable = ctable.dataTable({
	        "lengthMenu": [
	               [10,20,30,40,50,100],
	               [10,20,30,40,50,100]
	        ],
	        "pageLength": 10,
	        "language": {
	        	"processing": "数据加载中...",
	            "emptyTable": "暂无数据",
	            "info": "第_START_到_END_条记录,共_TOTAL_条记录",
	            "infoEmpty": "没有找到记录",
	            "infoFiltered": "",
	            "lengthMenu": "每页显示 _MENU_ 条记录",
	            "search": "搜索:",
	            "zeroRecords": "未找到匹配记录",
	            "paginate": {
	                "previous":"上一页",
	                "next": "下一页",
	                "last": "末页",
	                "first": "首页"
	            }
	        },
	        "columnDefs": [
	            {'orderable': false,"data":"id",'targets': [0],'Sortable': false,"visible": false},
	            {'orderable': false,"data":"secret",'targets': [1],'Sortable': false,"visible": false},
	            {'orderable': false,"data":"name",'targets': [2],'Sortable': false,"title":"平台名称"},
	            {'orderable': false,"data":"createTime",'targets': [3],"title":"创建时间",
	                "render":function(data, type, full){
	                	if(full.createTime===""){
	                		return"<span style='color:red;'>无</span>";
	                    }else{
	                    	return full.createTime;
	                    }
	                }
	            },{'orderable': false,"data":"username",'targets': [4],"title":"创建人",
	                "render":function(data, type, full){
	                	if(full.username===""){
	                		return"<span style='color:red;'>无</span>";
	                    }else{
	                    	return full.username;
	                    }
	                }
	            },{'orderable': false,"data":"status",'targets': [5],"title":"状态",
	                "render":function(data, type, full){
	                	if(full.status==0){
	                		return"<span class='label label-success'>可用</span>";
	                    }else if(full.status==1){
	                    	return"<span class='label label-danger'>不可用</span>";
	                    }else if(full.status==2){
	                    	return"<span class='label label-warning'>待审核</span>";
	                    }
	                }
	            },
	           {'orderable': false,'targets': [6],"title":"操作",
	          	 "render":function(data,type,full){
	          		 // -1、删除 0、可用 1、申请中 2、暂停使用
	          		 var btn ="&nbsp;<button class='btn btn-danger' type='button' id='notPassBtn'><i class='fa fa-ban'></i> 不通过</button>";
	          		 btn +="&nbsp;<button class='btn btn-info' type='button' id='passBtn'><i class='fa fa-check'></i> 通过</button>";
	          		 return btn;
	          	 }
	           },
	        ],
	        "bAutoWidth":false,
	        "bProcessing": true, // 是否显示取数据时的那个等待提示
		    "bServerSide": true,//这个用来指明是通过服务端来取数据
	        "sAjaxSource":"../../admin/branch/get/2",//请求的地址
	        "fnServerData": function(sSource,aoData,fnCallback){				  // 获取数据的处理函数
	        	$.ajax({
	        		url:sSource,
	        		type : "post",
	        		data:{
	        			"aoData":JSON.stringify(aoData)
	        		},
	        		dataType : "json",
	        		async:false,
	        		success:function(result){
	        			if(result.success){
	        				fnCallback(result.obj);
	        			}else{
	        			}
	        		},error:function(msg){
	        			if(a.readyState==4){
		    				window.location.href="../admin/loginpage";
		    			}else{
		    				toastr.error("系统异常！");
		    			}
	        		}
	        	});
	        } 
	    });
		
		//通过
		ctable.on("click","#passBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
	    	$.ajax({
	    		url:"../../admin/branch/pass",
				type:"post",
				dataType:"json",
				data:{"id":dData.id},
				success : function(data){
					if(data.success){
						toastr.success("通过成功！");
						ctable.fnDraw(true);
					}else{
						toastr.success("通过失败！");
					}
				},error:function(){
					if(a.readyState==4){
	    				window.location.href="../../admin/loginpage";
	    			}else{
	    				toastr.error("系统异常！");
	    			}
				}
	    	});
		});
		
		//不通过
		ctable.on("click","#notPassBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
	    	$.ajax({
	    		url:"../../admin/branch/notpass",
				type:"post",
				dataType:"json",
				data:{"id":dData.id},
				success : function(data){
					if(data.success){
						toastr.success("不通过！");
						ctable.fnDraw(true);
					}else{
						toastr.success("操作失败！");
					}
				},error:function(){
					if(a.readyState==4){
	    				window.location.href="../../admin/loginpage";
	    			}else{
	    				toastr.error("系统异常！");
	    			}
				}
	    	});
	    	
		});
		
	};
	
	return {
		init : function(){
			handleTable();
		} 
	};
}();
branch.init();
